package linker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import data.SimpleStatement;
import data.SimpleURI;
import data.Statement;

public class PrefixMap {
	private String src;
	private String tar;

	public PrefixMap(String preSrc, String preTar) {
		src = preSrc;
		tar = preTar;
	}

	public Statement map(Statement s) {
		SimpleURI sub;
		SimpleURI obj;
		SimpleURI pred;
		if (s.getSubject().getPrefix().equals(src)) {
			sub = new SimpleURI(tar, s.getSubject().getSurfix());
		} else {

			sub = new SimpleURI(s.getSubject().getPrefix(), s.getSubject()
					.getSurfix());
		}

		if (s.getPredicate().getPrefix().equals(src)) {
			pred = new SimpleURI(tar, s.getPredicate().getSurfix());
		} else {
			pred = new SimpleURI(s.getPredicate().getPrefix(), s.getPredicate()
					.getSurfix());
		}

		if (s.getObject().getPrefix().equals(src)) {
			obj = new SimpleURI(tar, s.getObject().getSurfix());
		} else {
			obj = new SimpleURI(s.getObject().getPrefix(), s.getObject()
					.getSurfix());

		}

		return new SimpleStatement(sub, pred, obj);
	}

	public static void main(String[] args) {
		
		if(args.length<1)
		{
			System.out.println("please specify the path for config file as parameter");
			return;
			
		}
		String config = args[0];
		Properties pre = new Properties();
		try {
			pre.load(new FileInputStream(config));
		} catch (FileNotFoundException e) {
			System.out.println("cannot find config file");
			return;
		} catch (IOException e) {
			System.out.println("cannot open config file");
			return;
		}
		String output = pre.getProperty("output");
		String input = pre.getProperty("input");
		
		ArrayList<PrefixMap> maps = new ArrayList<PrefixMap>();
		for (Object key : pre.keySet()) {
			if (!((String) key).equals("output")
					&& !((String) key).equals("input"))
				maps.add(new PrefixMap((String) key, pre
						.getProperty((String) key)));
		}
		Parser par = new Parser(null);
		try {
			par.open(input);
		} catch (FileNotFoundException e) {
			System.out.println("cannot find input file");
			return;
		} catch (IOException e) {
			System.out.println("cannot open input file");
			return ;
		}
		File outFile= new File(output);
		if(!outFile.exists())
			try {
				outFile.createNewFile();
			} catch (IOException e1) {
				System.out.println("cannt create output file");
				return;
			}
		FileOutputStream out;
		try {
			out= new FileOutputStream(outFile);
		} catch (FileNotFoundException e) {
			System.out.println("cannot find output file");
			return;
		}
		

		System.out.println("processing...");
		while (par.hasNext()) {
			Statement s= par.nextStatement();
			for(PrefixMap map:maps)
			{
				s=map.map(s);
			}
			try {
				out.write(s.toString().getBytes());
			} catch (IOException e) {
				System.out.println("Write error");
			}
		}
		System.out.println("mapping complete");
	}
}
